package com.gzz.orchid.main.facade.openfeign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpRequestInitializer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.client.support.RestTemplateAdapter;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.util.DefaultUriBuilderFactory;

import java.util.List;

/**
 * Remote 远端
 * // 构建接口的代理，可以注册为Bean，直接调用
 * RepositoryService service = factory.createClient(RepositoryService.class);
 *
 * @author higmd
 */
public class HttpExchangeAutoConfig {
    public HttpServiceProxyFactory serviceProxyFactory() {
        RestClient restClient = RestClient.builder()
                .defaultHeader("Content-Type", "application/json")
                .defaultStatusHandler(HttpStatusCode::isError, (req, resp) -> {
                    throw new RuntimeException("请求失败");
                })
                .baseUrl("http://localhost:8080").build();
        // 适配
        RestClientAdapter adapter = RestClientAdapter.create(restClient);
        return HttpServiceProxyFactory.builder().exchangeAdapter(adapter).build();
    }

    public HttpServiceProxyFactory httpServiceProxyFactory() {
        WebClient webClient = WebClient.builder()
                .defaultHeader("Content-Type", "application/json")
                .defaultStatusHandler(HttpStatusCode::isError, (response) -> {
                    throw new RuntimeException("请求失败");
                })
                .baseUrl("http://localhost:8080").build();
        // 适配
        WebClientAdapter adapter = WebClientAdapter.create(webClient);
        return HttpServiceProxyFactory.builder().exchangeAdapter(adapter).build();
    }

    public HttpServiceProxyFactory httpServiceProxyFactory2() {
        // 为RestTemplate
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://api.github.com/"));
        // 适配
        RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);
        return HttpServiceProxyFactory.builder().exchangeAdapter(adapter).build();
    }

    /**
     * 直接用 List<ClientHttpRequestInitializer> 自动注入了所有的拦截器，也可以自己去new一些，不用自动注入
     *
     */
    @Bean
    public HttpServiceProxyFactory httpServiceProxyFactory(RestTemplate restTemplate, @Autowired(required = false) List<ClientHttpRequestInitializer> requestInitializers) {
        // http client默认配置，需要更多配置就自己自定义。
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        var builder = RestClient.builder()
                .requestFactory(factory);
        for (ClientHttpRequestInitializer requestInitializer : requestInitializers) {
            builder.requestInitializer(requestInitializer);
        }

        RestClientAdapter adapter1 = RestClientAdapter.create(builder.build());
        return HttpServiceProxyFactory
                .builderFor(adapter1)
                .build();
    }
}
